<?php
class Ms_aspeknilai extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('master_model');
		$this->load->model('pelajaran_model');
	}
	
	function index($kode_mp,$id_sekolah,$id_ta)
	{
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		// if($this->session->userdata('roleid')=='0') //diknas
			// $colModel['ID_SEKOLAH'] = array('Nama Sekolah',100,TRUE,'center',1);
		$colModel['TAHUN_AJARAN'] = array('Tahun Ajaran',100,TRUE,'center',1);
		$colModel['NAMA_MATA_PELAJARAN'] = array('Kode Mata Pelajaran',100,TRUE,'center',1);
		$colModel['KODE_PENILAIAN'] = array('Kode Penilaian',100,TRUE,'center',1);
		$colModel['ASPEK_PENILAIAN'] = array('Aspek Penilaian',200,TRUE,'center',1);
		$colModel['KETERANGAN'] = array('Keterangan',300,TRUE,'center',1);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		// $colModel['JABATAN'] = array('Jabatan Tertinggi',400,TRUE,'center',1);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Aspek Penilaian'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		$buttons[] = array('Add','add','org_js');
		$buttons[] = array('Delete','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Back to Pelajaran','back','org_js');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/ms_aspeknilai/listaspeknilai_grid/".$kode_mp."/".$id_sekolah."/".$id_ta),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Add')
				{
					location.href='".base_url()."index.php/ms_aspeknilai/input_aspeknilai/".$kode_mp."/".$id_sekolah."/".$id_ta."'; 
				}
				if (com=='Back to Pelajaran')
				{
					location.href='".base_url()."index.php/ms_pelajaran/'; 
				}
				if (com=='Delete')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/ms_aspeknilai/delete_aspeknilai')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listaspeknilai_grid($kode_mp,$id_sekolah,$id_ta)
	{
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		if($this->session->userdata('roleid')=='0') //diknas
			$valid_fields = array('NAMA_SEKOLAH','NAMA_TA','KODE_MATA_PELAJARAN','KODE_PENILAIAN','ASPEK_PENILAIAN','KET');
		else	
			$valid_fields = array('NAMA_TA','KODE_MATA_PELAJARAN','KODE_PENILAIAN','ASPEK_PENILAIAN','KET');
		
		$this->flexigrid->validate_post('KODE_PENILAIAN','asc',$valid_fields);

		$records = $this->pelajaran_model->get_aspeknilai($kode_mp,$id_sekolah,$id_ta);
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			if($this->session->userdata('roleid')=='0'){ //diknas
				$record_items[] = array($row->ID_ASPEK,
				$no+1,
				'<div align="left">'.$row->NAMA_SEKOLAH.'</div>',
				'<div align="left">'.$row->NAMA_TA.'</div>',
				'<div align="left">'.$row->NAMA_MATA_PELAJARAN.'</div>',
				'<div align="left">'.$row->KODE_PENILAIAN.'</div>',
				'<div align="left">'.$row->ASPEK_PENILAIAN.'</div>',
				'<div align="left">'.$row->KETERANGAN.'</div>',
				'<a href=\''.site_url().'/ms_aspeknilai/edit_aspeknilai/'.$row->ID_ASPEK.'/'.str_replace('/','-',$row->ID_TA).'/'.$row->KODE_MATA_PELAJARAN.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
				);
				$no++;
			}else{
				$record_items[] = array($row->ID_ASPEK,
				$no+1,
				'<div align="left">'.$row->NAMA_TA.'</div>',
				'<div align="left">'.$row->NAMA_MATA_PELAJARAN.'</div>',
				'<div align="left">'.$row->KODE_PENILAIAN.'</div>',
				'<div align="left">'.$row->ASPEK_PENILAIAN.'</div>',
				'<div align="left">'.$row->KETERANGAN.'</div>',
				'<a href=\''.site_url().'/ms_aspeknilai/edit_aspeknilai/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta.'/'.$row->ID_ASPEK.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
				);
				$no++;
			}
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			// $this->form_validation->set_rules('tahun_ajaran', 'Tahun Ajaran', 'required');
			// $this->form_validation->set_rules('kode_mp', 'Kode Mata Pelajaran', 'required');
			$this->form_validation->set_rules('kode_penilaian', 'Kode Penilaian', 'required');
			$this->form_validation->set_rules('aspek_penilaian', 'Aspek Penilaian', 'required');
			// if($this->session->userdata('roleid')=='0') //diknas
				// $this->form_validation->set_rules('id_sekolah', 'Nama Sekolah', 'required');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function cek_kodenilai($kode_mp, $id_sekolah, $id_ta, $kode_nilai){
		$rs_cek = $this->pelajaran_model->validasi_aspeknilai($kode_mp, $id_sekolah, $id_ta, $kode_nilai)->row();
		return $rs_cek;
	}
	
	function input_aspeknilai($kode_mp, $id_sekolah, $id_ta)
    {
		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'new';
			$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
			$data['sekolah'] = $this->master_model->combo_sekolah();
			$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
			$data['tahunajaran'] = $this->master_model->combo_tahunajaran_kelas($this->session->userdata('unitid'));
			$data['kodemp'] = $this->pelajaran_model->combo_mp($data['tahunajaran']->row()->ID_TA);
			$data['content'] = $this->load->view('form_aspeknilai',$data,true);
			$this->load->view('main',$data);
		}
		else{
			// if($this->cek_kodenilai($this->session->userdata('unitid'),$this->input->post('tahun_ajaran'),$this->input->post('kode_mp'),$this->input->post('kode_penilaian'))->jml > 0){ //sdh ada di database, reject
			if($this->cek_kodenilai($kode_mp, $id_sekolah, $id_ta, $this->input->post('kode_penilaian'))->jml > 0){ //sdh ada di database, reject
				$data['status'] = 'new';
				$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
				$data['sekolah'] = $this->master_model->combo_sekolah();
				$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
				$data['tahunajaran'] = $this->master_model->combo_tahunajaran_kelas($this->session->userdata('unitid'));
				$data['kodemp'] = $this->pelajaran_model->combo_mp($data['tahunajaran']->row()->ID_TA);
				
				$data['value'] = $this->input->post();
				$data['err_msg'] = 'Kode Penilaian telah ada dalam database.';
				$data['content'] = $this->load->view('form_aspeknilai',$data,true);
				$this->load->view('main',$data);
			}else
				$this->insert_aspeknilai($kode_mp, $id_sekolah, $id_ta);
		}            
    }
	
	function insert_aspeknilai($kode_mp, $id_sekolah, $id_ta){
		if($this->session->userdata('roleid')=='0'){ //diknas
			$id_sekolah = $this->input->post('id_sekolah');
		}else{
			$id_sekolah = $this->session->userdata('unitid');
		}
		$id_aspek = $this->pelajaran_model->getLastAspek()->row();
		$data = array(
			// 'ID_ASPEK'				=> $id_aspek->maxid +1,
			'TAHUN_AJARAN'			=> $this->input->post('tahun_ajaran'),
			'ID_SEKOLAH'			=> $id_sekolah,
			'KODE_MATA_PELAJARAN'	=> $this->input->post('kode_mp'),	
			'KODE_PENILAIAN'		=> $this->input->post('kode_penilaian'),	
			'ASPEK_PENILAIAN'		=> $this->input->post('aspek_penilaian'),	
			'KETERANGAN'	 		=> $this->input->post('keterangan')
		);
		$this->pelajaran_model->insert_aspeknilai($data);
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('ms_aspeknilai/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta); 
	}
	
	function edit_aspeknilai($kode_mp, $id_sekolah,$id_ta,$id_aspek)
    {
 		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'edit';
			$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
			$data['sekolah'] = $this->master_model->combo_sekolah();
			$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
			$data['tahunajaran'] = $this->master_model->combo_tahunajaran_kelas($this->session->userdata('unitid'));
			$data['kodemp'] = $this->pelajaran_model->combo_mp($data['tahunajaran']->row()->ID_TA);
			
			$data['rs_aspeknilai'] = $this->pelajaran_model->get_aspeknilaiID($id_aspek);
			$data['content'] = $this->load->view('form_aspeknilai',$data,true);
			$this->load->view('main',$data);
		}
		else{
			// var_dump($this->input->post());var_dump($kodeaspek);exit;
			if($this->cek_kodenilai($kode_mp, $id_sekolah, $id_ta, $this->input->post('kode_penilaian'))->jml > 0 and $this->cek_kodenilai($kode_mp, $id_sekolah, $id_ta, $this->input->post('kode_penilaian'))->ID_ASPEK != $id_aspek){ //sdh ada di database, reject
				$data['status'] = 'edit';
				$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
				$data['sekolah'] = $this->master_model->combo_sekolah();
				$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
				$data['tahunajaran'] = $this->master_model->combo_tahunajaran_kelas($this->session->userdata('unitid'));
				$data['kodemp'] = $this->pelajaran_model->combo_mp($data['tahunajaran']->row()->ID_TA);
				
				$data['rs_aspeknilai'] = $this->pelajaran_model->get_aspeknilaiID($id_aspek);
				$data['err_msg'] = 'Kode Penilaian telah ada dalam database.';
				$data['content'] = $this->load->view('form_aspeknilai',$data,true);
				$this->load->view('main',$data);
			}else
				$this->update_aspeknilai($kode_mp,$id_sekolah,$id_ta,$id_aspek);
		}            
    }
	
	function update_aspeknilai($kode_mp,$id_sekolah,$id_ta, $id_aspek){
		if($this->session->userdata('roleid')=='0'){ //diknas
			$id_sekolah = $this->input->post('id_sekolah');
		}else{
			$id_sekolah = $this->session->userdata('unitid');
		}
		
		$data = array(
			// 'TAHUN_AJARAN'			=> $this->input->post('tahun_ajaran'),
			// 'ID_SEKOLAH'			=> $id_sekolah,
			// 'KODE_MATA_PELAJARAN'	=> $this->input->post('kode_mp'),	
			'KODE_PENILAIAN'		=> $this->input->post('kode_penilaian'),	
			'ASPEK_PENILAIAN'		=> $this->input->post('aspek_penilaian'),	
			'KETERANGAN'	 		=> $this->input->post('keterangan')
		);
		
		$this->pelajaran_model->update_aspeknilai($data, $id_aspek);
		$this->session->set_flashdata('suc_msg', 'Data berhasil diupdate.');
		redirect('ms_aspeknilai/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta); 
	}
	
	//Delete
	function delete_aspeknilai()
	{
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				$this->pelajaran_model->delete_aspeknilai($ta_id);
				$msg .= "Aspek Penilaian berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
    
}
?>
